package com.woniuxy.book87.controller;

import com.woniuxy.book87.dto.AccountDto;
import com.woniuxy.book87.entity.Manager;
import com.woniuxy.book87.mapper.ManagerMapper;
import com.woniuxy.book87.result.Result;
import com.woniuxy.book87.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;

@RestController  // @Controller  +  @ResponseBody
@RequestMapping("manager")
@Slf4j
public class ManagerController {
    @Autowired
    ManagerService managerService ;
    @PostMapping("/login")
    //@RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result<Manager> login(@RequestBody AccountDto accountDto){
        log.info("接收的用户名和密码:{},{}",accountDto.getUsername(),accountDto.getPassword());
        Manager manager = managerService.login(accountDto.getUsername(), accountDto.getPassword());
        if (manager != null) {
            //session中保存 用户信息
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();

            request.getSession().setAttribute("ManagerInfo",manager);
          return Result.ok("登陆成功",manager);
        }else {
            return Result.fail("登录失败");
        }
    }
}
